﻿using CreateCodeByDatabase.Model;
using ReadModel.MSIL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CreateCodeByDatabase.Bll
{
    public abstract class IOperate
    {
        /// <summary>
        /// 根据合约号查询园机入库信息
        /// </summary>
        /// <param name="hyh">合约号</param>
        /// <returns></returns>
        public abstract IEnumerable<TableInfo> QueryTableList(string tableName);
        /// <summary>
        /// 获取指定表列信息
        /// </summary>
        /// <param name="tableNameList">指定表名集合</param>
        /// <returns></returns>
        public abstract IEnumerable<ColumnInfo> GetTableColumnList(IEnumerable<string> tableNameList);
        public abstract int ExecuteSql(string sql, List<string> aloneExecuteSqlList = null);

        protected static IEnumerable<T> Query<T>(IDataReader reader)
        {
            Type effectiveType = typeof(T);
            Func<IDataReader, object> func = ILHandler.GetTypeDeserializerImpl(effectiveType, reader, 0, -1, false);
            var convertToType = Nullable.GetUnderlyingType(effectiveType) ?? effectiveType;
            while (reader.Read())
            {
                object val = func(reader);
                if (val == null || val is T)
                {
                    yield return (T)val;
                }
                else
                {
                    yield return (T)Convert.ChangeType(val, convertToType, CultureInfo.InvariantCulture);
                }
            }
        }

        public static enumConnectionType ConnectionType { get; set; }
        public static IOperate GetOperate()
        {
            if (ConnectionType == enumConnectionType.Oracle)
                return new Operate_Ora();
            else if( ConnectionType == enumConnectionType.SqlServer )
                return new Operate_Sql();
            else if (ConnectionType == enumConnectionType.MySql)
                return new Operate_Mysql();
            return new Operate_Ora();
        }
    }
}
